local times = {}

local function timer(name, f)
	local start = os.clock()
	f()
	local stop = os.clock()

	times[name] = (times[name] or 0) + (stop - start)
end

local function show_timers()
	for n,t in pairs(times) do
		print(n .. ": " .. t .. " sec")
	end
end

function gfx_test()
	local w, h = 800, 600

	graphics.fsaa(4)
	
	graphics.video(w, h)

	graphics.clear_color(0, 0, 0)

	local world = physics.create_world()

	local snd = sound.load("test.wav")

	--[[
	local img = graphics.create_image(100, 100)

	for y=0,99 do
		for x=0,99 do
			local d = (x*x + y*y) * 255 / (100*100*2)
			img:setpixel(x, y, d, 255, 255)
		end
	end]]

	local font = graphics.load_font("../Vera.ttf", 64*40)

	local img = graphics.load_image("../face.png")

	local spr = img:to_sprite()

	for y=-50,30,8 do
		for x=0,100,4 do
			local body = world:create_body(x+y/10, y)
			body:add_circle(0, 0, 2)
			--body:add_box(0, 0, 4, 4)
		end
	end

	for i=-10,100,7 do
		local body = world:create_body(i, 50, true)
		body:add_circle(0, 0, 2)
		--body:add_box(0, 0, 4, 4)
	end

	local temp_world

	local frame = 0

	local offsx = w/16 - w/2
	local offsy = h/16 - h/2
	local zoom = 8

	while graphics.update() do
	--[[
		while true do
			local ev, data = graphics.next_event()
			if not ev then break end

			if ev == "button" then
				print("Button: " .. data.id, data.state)
				if data.id == graphics.kbEscape then
					return
				end
			end
		end]]

		if graphics.buttons[graphics.kbEscape] then
			graphics.close()
		end

		if graphics.buttons[graphics.kbLeft] then
			sound.play(snd)
			graphics.buttons[graphics.kbLeft] = false
		end

		timer("world step", function()
			world:step(1/30)
		end)
		if temp_world then
			temp_world:step(1/30)
		end

		graphics.reset_transform()

		graphics.clear()

		graphics.color(1, 1, 1)

		--[[
		do
			graphics.begin_lines()

			graphics.line(0, 0, 200, 500)
		end

		do
			graphics.begin_triangle_fan()

			graphics.vertex(100, 100)
			graphics.vertex(200, 100)
			graphics.vertex(250, 200)
			graphics.vertex(150, 250)
		end
		]]

		do
			graphics.begin_points()

			graphics.color(1, 0, 0)

			for x = 1,100 do
				graphics.vertex(x, x)
			end
		end

		graphics.begin_sprites()

		graphics.color(1, 1, 1)

		--[[
		for y=1,100 do
			graphics.text(font, "Hello", y, y)
		end
		]]
		

		graphics.zoom(offsx, offsy, zoom)

		if graphics.buttons[graphics.kbA] then
			offsx = offsx - 1
		elseif graphics.buttons[graphics.kbD] then
			offsx = offsx + 1
		end

		if graphics.buttons[graphics.kbW] then
			offsy = offsy - 1
		elseif graphics.buttons[graphics.kbS] then
			offsy = offsy + 1
		end

		if graphics.buttons[graphics.msWheelUp] then
			zoom = zoom * 1.5
			graphics.buttons[graphics.msWheelUp] = false
		elseif graphics.buttons[graphics.msWheelDown] then
			zoom = zoom / 1.5
			graphics.buttons[graphics.msWheelDown] = false
		end

		do
			--local temp_world = physics.create_world(world)
			--local temp_world = world
			local render_world = world
			
			--[[
			if temp_world and frame % 2 == 0 then
				render_world = temp_world
			end
			]]

			local bodies = {}
			for _,body in pairs(render_world.id_to_body) do
				bodies[#bodies + 1] = body
			end

			for i=1,#bodies do
				local px, py, ang = bodies[i]:orientation()
				--graphics.sprite_centered_rot(spr, px*10, py*10, ang + 0.3)
				graphics.sprite_centered_rot(spr, px, py, ang + 0.3, 0.1)
			end
			

			--[[
			do
				local testbody = render_world.id_to_body[20]
				for i=1,100 do
					local px, py, ang = testbody:orientation()
					graphics.sprite_centered_rot(spr, px*10, py*10, ang)
				end
			end]]
		end

		--[[
		for x=1,1000,10 do
			graphics.sprite_centered(spr, x, x)
		end
		]]

		graphics.throttle_fps(30)
		graphics.end_drawing()

		frame = frame + 1

		if frame == 30*8 then
			--temp_world = physics.create_world(world)
			--break
		end
	end

	show_timers()

	print("ok")
end